package com.ts.proj.service.impl;

import com.ts.proj.entity.ErrorprojectEntity;
import com.ts.proj.repository.ErrorprojectRepository;
import com.ts.proj.service.ErrorprojectService;
import com.ts.util.EntityAttributeUtil;
import com.ts.util.PageData;

import com.ts.base.service.impl.CommonServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERErrorproject                Errorproject

@Service
public class ErrorprojectServiceImpl extends CommonServiceImpl<ErrorprojectEntity>  implements ErrorprojectService {

    @Autowired
    private ErrorprojectRepository errorprojectRepository;

    @Override
    public Specification getSpecification(PageData pageData, Class entityClass) {
        return new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList();
                List<String> fieldList = EntityAttributeUtil.entityAttribute(entityClass);

                preList.add(criteriaBuilder.equal(root.get("deleteFlag"),"N"));
                if(null != pageData.get("userId") && !"".equals(pageData.get("userId").toString())) {
                    preList.add(criteriaBuilder.equal(root.get("userId"), pageData.get("userId").toString()));
                }
                if(null != pageData.get("projectId") && !"".equals(pageData.get("projectId").toString())) {
                    preList.add(criteriaBuilder.equal(root.get("projectId"), pageData.get("projectId").toString()));
                }
                if(null != pageData.get("belongTo") && !"".equals(pageData.get("belongTo").toString())){
                    preList.add(criteriaBuilder.equal(root.get("belongTo"), pageData.get("belongTo").toString()));
                }
                if(null != pageData.get("projectName") && !"".equals(pageData.get("projectName").toString())){
                    preList.add(criteriaBuilder.like(root.get("projectName"), "%"+(pageData.get("projectName").toString())+"%"));
                }
                if(null != pageData.get("uuid") && !"".equals(pageData.get("uuid").toString())){
                    preList.add(criteriaBuilder.equal(root.get("uuid"), pageData.get("uuid").toString()));
                }
                if(null != pageData.get("vetoFlag") && !"".equals(pageData.get("vetoFlag").toString())){
                    preList.add(criteriaBuilder.equal(root.get("vetoFlag"), pageData.get("vetoFlag").toString()));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and((Predicate[])preList.toArray(p));
            }
        };
    }

    @Override
    public List<ErrorprojectEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<ErrorprojectEntity> sp = this.getSpecification(pageData,ErrorprojectEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return errorprojectRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<ErrorprojectEntity> sp = this.getSpecification(pageData,ErrorprojectEntity.class);
        return errorprojectRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<ErrorprojectEntity> iterable) {
        errorprojectRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<ErrorprojectEntity> saveAll(List<ErrorprojectEntity> entities) {
        return errorprojectRepository.saveAll(entities);
    }

    @Override
    public List<ErrorprojectEntity> findAllById(Integer[] ids) {
        List<ErrorprojectEntity> list = errorprojectRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            ErrorprojectEntity entity = new ErrorprojectEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
		errorprojectRepository.deleteAll();
        return false;
    }
}